Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Script.Serialization
Imports System.ComponentModel
Imports System.Collections.Generic
Imports Objects
Imports Objects.EmployeeInfo
Imports Objects.Security
<Assembly: WebResource("AjaxControlLib.EmployeeLookup.js", "text/javascript")> 



Public Class EmployeeLookup

    Inherits WebControl
    Implements INamingContainer

    Private _SM As ScriptManager
    Private _lblUpdate As Label
    Public Event OnChange As EventHandler
    Public Event OnClick(ByVal loemp As Employee)
    Public Event OnMaxResults()
    Public Event OnProcessingCountryChange(ByVal psProcessingCountry As String)
    Private _SecurityGroupID As Integer = 0
    Private _Population As ePopulation = ePopulation.WholeFirm
    Private _AutoSearch As Boolean = False
    Private _SelectedEmployee As Employee
    Private _SearchStringMin As Integer = 1
    Private _Label As String = "Name: "
    Private _CssClass As String = ""
    Private _MaxResults As Integer = 0
    Private _NameFieldWidth As Integer = 0
    Private _ProcessingCountry As String

    Public Property Label() As String
        Get
            Return _Label
        End Get
        Set(ByVal value As String)
            _Label = value
        End Set
    End Property
    Public Property MaxResults() As Integer
        Get
            Return _MaxResults
        End Get
        Set(ByVal value As Integer)
            _MaxResults = value
        End Set
    End Property

    Public Property AutoSearch() As Boolean
        Get
            Return _AutoSearch
        End Get
        Set(ByVal value As Boolean)
            _AutoSearch = value
        End Set
    End Property
    Public Property SearchStringMinimum() As Integer
        Get
            Return _SearchStringMin
        End Get
        Set(ByVal value As Integer)
            _SearchStringMin = value
        End Set
    End Property
    Public Overrides Property CssClass() As String
        Get
            Return _CssClass
        End Get
        Set(ByVal value As String)
            _CssClass = value
        End Set
    End Property
    Public Property Text() As String
        Get
            Dim loCtl As TextBox = CType(FindControl("_Employee"), TextBox)

            Return loCtl.Text
        End Get
        Set(ByVal value As String)
            Dim txtEmployee As TextBox = CType(FindControl("_Employee"), TextBox)

            txtEmployee.Text = value
        End Set
    End Property
    Public Property value() As String
        Get
            Dim loGUI As HiddenField = CType(FindControl("_GUI"), HiddenField)

            Return loGUI.Value
        End Get
        Set(ByVal value As String)
            CType(FindControl("_GUI"), HiddenField).Value = value
        End Set
    End Property

    Public Property ProcessingCountry() As String
        Get
            Return _ProcessingCountry
        End Get
        Set(ByVal value As String)
            _ProcessingCountry = value
        End Set
    End Property

    Public Enum ePopulation
        WholeFirm
        WholeFirmComplete
        GSSDeployedCountries
        SecurityGroup
        WholeFirmActive
        WholeFirmCompleteNotTerminated
        FirmByProcessingCountry
        FirmByProcessingCountryComplete
        FirmByProcessingCountryNotTerminated
        FirmByProcessingCountryCompleteNotTerminated
        EmployeeSearchIndiaOnly
    End Enum

    Public Property SecurityGroupID() As Integer
        Get
            Return _SecurityGroupID
        End Get
        Set(ByVal value As Integer)
            _SecurityGroupID = value
        End Set
    End Property

    Public Property Population() As ePopulation

        Get
            Return _Population
        End Get
        Set(ByVal value As ePopulation)
            _Population = value
        End Set
    End Property

    Public WriteOnly Property NameFieldWidth() As Integer
        Set(ByVal value As Integer)
            _NameFieldWidth = value
        End Set
    End Property

    Protected Overrides Sub CreateChildControls()
        Dim parent As Control
        Dim container As Control
        If SM Is Nothing OrElse Not SM.EnablePartialRendering Then
            container = Me
            parent = Me
        Else
            Dim up As UpdatePanel = New UpdatePanel
            up.ID = "_UpPan"
            container = up.ContentTemplateContainer
            parent = up
            up.UpdateMode = UpdatePanelUpdateMode.Conditional
        End If



        Dim txtEmployee As TextBox = New TextBox
        txtEmployee.ID = "_Employee"
        txtEmployee.AutoCompleteType = AutoCompleteType.None
        txtEmployee.Attributes.Add("AUTOCOMPLETE", "off")
        txtEmployee.CssClass = _CssClass
        txtEmployee.ToolTip = "Last Name, First"
        If _NameFieldWidth > 0 Then
            txtEmployee.Width = Unit.Pixel(_NameFieldWidth)
        End If
        Dim txtGUI As HiddenField = New HiddenField
        txtGUI.ID = "_GUI"
        container.Controls.Add(txtGUI)
        If AutoSearch Then
            txtEmployee.Attributes.Add("onkeyup", "javascript:onTxtKeyUp(this);")
            txtEmployee.Attributes.Add("onkeydown", "javascript:onTxtKeyDown();")
            AddHandler txtEmployee.TextChanged, AddressOf txtEmployee_OnChanged
        End If

        Dim divscroll As Panel = New Panel
        divscroll.Height = New Unit("200px")
        divscroll.Width = New Unit("250px")
        divscroll.ScrollBars = ScrollBars.Vertical
        divscroll.ID = "_Scroll"
        Dim tabResults As Table = New Table
        tabResults.ID = "_Results"
        tabResults.CellPadding = 2
        tabResults.CellSpacing = 0
        tabResults.BorderWidth = 0
        tabResults.Width = New Unit("233px")
        divscroll.Style.Add("display", "none")
        divscroll.Style.Add("position", "absolute")
        divscroll.Style.Add("z-index", "10000")
        divscroll.Style.Add("background-color", "white")

        divscroll.CssClass = "ShadowBox"

        Dim cmdClose As HtmlControls.HtmlGenericControl = New HtmlControls.HtmlGenericControl
        cmdClose.ID = "cmdClose"
        cmdClose.InnerHtml = "<img id='imgClose' src='/CMT/_includes/images/Stop_hov1.gif' />"
        cmdClose.Attributes.Add("Title", "Close list without selection")
        cmdClose.Style.Add("position", "relative")
        cmdClose.Style.Add("top", "10")
        cmdClose.Style.Add("left", "215")
        cmdClose.Style.Add("z-index", "100")
        cmdClose.Style.Add("color", "red")
        cmdClose.Style.Add("font-weight", "bold")
        cmdClose.Style.Add("cursor", "hand")
        cmdClose.Attributes.Add("onclick", "OnClose(this)")

        divscroll.Controls.Add(cmdClose)
        divscroll.Controls.Add(tabResults)
        Dim divlabel As New HtmlControls.HtmlGenericControl
        divlabel.InnerText = "Name:"
        divlabel.Style.Add("width", "100%")
        divlabel.InnerText = Label
        container.Controls.Add(divlabel)
        container.Controls.Add(txtEmployee)

        ScriptManager.RegisterStartupScript(Page, GetType(Page), "Positioning", "PositionList(""" & ClientID & "_" & txtEmployee.ClientID & """,""" & ClientID & "_" & divscroll.ClientID & """);", True)
        container.Controls.Add(divscroll)

        If Not AutoSearch Then
            Dim cmdSearch As ImageButton = New ImageButton
            cmdSearch.ID = "_SearchCmd"
            cmdSearch.CommandName = "Change"
            cmdSearch.ImageUrl = "/CMT/_includes/images/magnify.gif"
            cmdSearch.OnClientClick = "this.src = '/CMT/_includes/images/prog.gif'"
            container.Controls.Add(cmdSearch)
        Else
            Dim prog As UpdateProgress = New UpdateProgress
            prog.ID = "prog"
            prog.ProgressTemplate = New ProgressTemplate
            prog.AssociatedUpdatePanelID = parent.ID

            container.Controls.Add(prog)

        End If

        If parent.ID <> ID Then
            Controls.Add(parent)
        End If

        If SM.AsyncPostBackSourceElementID.IndexOf("SELECTED") >= 0 Then
            Dim loemp As Employee = GetSelectedEmployee()
            Dim lnk As LinkButton = CreateLink(loemp.GUI, loemp.CommonName, loemp.GPN)
            lnk.Visible = True
            Controls.Add(lnk)
        End If

    End Sub

    Private Function GetSelectedEmployee() As Employee
        If _SelectedEmployee Is Nothing Then
            Dim psGPN As String = SM.AsyncPostBackSourceElementID.Substring(SM.AsyncPostBackSourceElementID.LastIndexOf("_") + 1)
            _SelectedEmployee = New Employee(psGPN)
        End If
        Return _SelectedEmployee
    End Function

    Private Sub txtEmployee_OnChanged(ByVal o As Object, ByVal e As EventArgs)
        BuildList()

        If _NameFieldWidth > 0 Then
            CType(o, TextBox).Width = _NameFieldWidth
        End If
        Dim args As CommandEventArgs = New CommandEventArgs("TextChanged", CType(o, TextBox).UniqueID)
        MyBase.RaiseBubbleEvent(Me, args)

    End Sub

    Private Sub txtEmployee_OnClick(ByVal o As Object, ByVal e As EventArgs)
        Dim args As CommandEventArgs = New CommandEventArgs("Clicked", CType(o, LinkButton).UniqueID)
        MyBase.RaiseBubbleEvent(Me, args)
    End Sub

    Private ReadOnly Property SM() As ScriptManager
        Get
            If _SM Is Nothing Then
                _SM = ScriptManager.GetCurrent(Page)
            End If
            Return _SM
        End Get
    End Property

    Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal e As EventArgs) As Boolean
        Dim handled As Boolean = False
        If TypeOf e Is CommandEventArgs Then
            Dim ce As CommandEventArgs = CType(e, CommandEventArgs)
            Select Case ce.CommandName
                Case "Change"
                    BuildList()
                    handled = True
                Case "Click"
                    OnClickHandler(ce)
                    CType(source, WebControl).Visible = False
                    handled = True
            End Select

        End If
        Return handled
    End Function

    Protected Overridable Sub OnClickHandler(ByVal e As EventArgs)
        Dim tb As TextBox = CType(FindControl("_Employee"), TextBox)
        Dim txtGUI As HiddenField = CType(FindControl("_GUI"), HiddenField)
        tb.Text = GetSelectedEmployee.Name
        txtGUI.Value = GetSelectedEmployee.GUI

        RaiseEvent OnClick(GetSelectedEmployee)
    End Sub

    Private Sub BuildList()
        Dim dr As Table = CType(FindControl("_Results"), Table)
        Dim ds As Panel = CType(FindControl("_Scroll"), Panel)
        Dim cc As HtmlControls.HtmlGenericControl = CType(FindControl("cmdClose"), HtmlControls.HtmlGenericControl)
        ds.Style.Add("font-weight", "bold")

        Dim tb As TextBox = CType(FindControl("_Employee"), TextBox)
        Dim lsValue As String = tb.Text
        If lsValue.Trim.Length = 0 Then
            ds.Style.Add("display", "none")
            cc.Style.Add("display", "none")
            Exit Sub
        End If


        Dim loBI As BusinessInfo = New BusinessInfo
        Dim ldtResults As DataTable = Nothing
        If lsValue.Length <= SearchStringMinimum Then
            Exit Sub
        End If
        Select Case Population
            Case ePopulation.WholeFirmActive
                If MaxResults > 0 Then
                    ldtResults = loBI.EmployeeSearchAllEmployees(lsValue, "EmploymentStatus='A'", MaxResults)
                Else
                    ldtResults = loBI.EmployeeSearchAllEmployees(lsValue, "EmploymentStatus='A'")
                End If
        End Select

        If ldtResults Is Nothing Then

            Exit Sub
        End If


        ds.Style.Add("display", "")
        cc.Style.Add("display", "")

        Dim liCount As Integer = 0
        If MaxResults > 0 And ldtResults.Rows.Count = MaxResults Then
            Dim lTR1 As TableRow = New TableRow
            Dim lTD1 As TableCell = New TableCell

            lTD1.ColumnSpan = 3

            lTD1.Style.Add("background", "white")
            lTD1.Style.Add("color", "red")
            lTD1.Style.Add("font-size", "8pt")
            lTD1.Width = New Unit("100%")
            lTD1.Text = "Results exceeded " & MaxResults.ToString & " row limit"
            lTR1.Cells.Add(lTD1)

            dr.Rows.Add(lTR1)

        Else

            If ldtResults.Rows.Count = 0 Then
                Dim lTR1 As TableRow = New TableRow
                Dim lTD1 As TableCell = New TableCell

                lTD1.ColumnSpan = 3

                lTD1.Style.Add("background", "white")
                lTD1.Style.Add("color", "red")
                lTD1.Style.Add("font-size", "8pt")
                lTD1.Width = New Unit("100%")
                lTD1.Text = "No results returned."
                lTR1.Cells.Add(lTD1)

                dr.Rows.Add(lTR1)
            End If

        End If

        For Each loRow As DataRow In ldtResults.Rows
            Dim lTR1 As TableRow = New TableRow
            Dim lTD1 As TableCell = New TableCell

            Dim lnk As LinkButton = CreateLink(loRow("GUI").ToString, loRow("CommonName").ToString, loRow("GPN").ToString)



            lTD1.Controls.Add(lnk)

            lTD1.ColumnSpan = 3

            Dim bgColor As String = ""
            If liCount Mod 2 = 0 Then
                bgColor = "white"

            Else
                bgColor = "silver"
            End If

            lTD1.Style.Add("background", bgColor)
            lTD1.Style.Add("font-size", "8pt")
            lTD1.Width = New Unit("100%")
            lTR1.Cells.Add(lTD1)

            dr.Rows.Add(lTR1)
            Dim lTR2 As TableRow = New TableRow
            Dim lTD2 As TableCell = New TableCell
            lTD2.Text = loRow("GPN").ToString
            lTD2.Style.Add("font-size", "6pt")

            lTD2.Style.Add("background", bgColor)
            lTR2.Cells.Add(lTD2)
            Dim lTD3 As TableCell = New TableCell
            lTD3.Text = loRow("Rank").ToString
            lTD3.Style.Add("font-size", "6pt")
            lTD3.Style.Add("background", bgColor)
            lTR2.Cells.Add(lTD3)
            Dim lTD4 As TableCell = New TableCell
            lTD4.Text = loRow("Coding").ToString
            lTD4.Style.Add("font-size", "6pt")
            lTD4.Style.Add("background", bgColor)
            lTD4.Wrap = False
            lTR2.Cells.Add(lTD4)
            dr.Rows.Add(lTR2)
            liCount += 1
        Next
        Dim loPnl As UpdatePanel = DirectCast(FindControl("_UpPan"), UpdatePanel)
        loPnl.Update()
    End Sub

    Public Function CreateLink(ByVal psGUI As String, ByVal psName As String, ByVal psGPN As String) As LinkButton
        Dim lnk As LinkButton = New LinkButton
        lnk.Attributes.Add("onclick", "HidePanel('" & CType(FindControl("_Scroll"), Panel).ClientID & "');")
        lnk.Text = psName
        lnk.CommandName = "Click"
        lnk.ID = "SELECTED_" & psGUI & "_" & psGPN
        lnk.CommandArgument = psGUI
        AddHandler lnk.Click, AddressOf txtEmployee_OnClick
        Return lnk
    End Function

    Private Sub EmployeeLookup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cc As HtmlControls.HtmlGenericControl = CType(FindControl("cmdClose"), HtmlControls.HtmlGenericControl)
        cc.Style.Add("display", "none")
        Dim pan As Panel = CType(FindControl("_Scroll"), Panel)
        pan.Style.Add("display", "none")
        Dim cs As ClientScriptManager = Page.ClientScript
        Dim psURL As String = cs.GetWebResourceUrl(Me.GetType, "AjaxControlLib.EmployeeLookup.js")
        cs.RegisterClientScriptInclude("EMP", psURL)
    End Sub


    Private Sub EmployeeLookup_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim divscroll As Panel = CType(FindControl("_Scroll"), Panel)
        divscroll.Attributes.Add("onmousewheel", "OnScroll('" & FindControl("cmdClose").ClientID & "');")
        divscroll.Attributes.Add("onscroll", "OnScroll('" & FindControl("cmdClose").ClientID & "');")
    End Sub


    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class
